package fr.web325.servlet.admin.sales;

import fr.web325.dao.SalesDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@WebServlet("/shop/admin/salesList")
public class SalesList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取时间分割
        /* 找出指定的2个字符在 该字符串里面的 位置 */
        String date = req.getParameter("date");
        if(Objects.equals(date, "")){
            req.getRequestDispatcher("sales/index.jsp").forward(req,resp);
            return;
        }
        int str1 = date.indexOf("年");
        int str2 = date.indexOf("月");
        try {
            String year =  date.substring(0, str1);
            String month =  date.substring(str1, str2).substring("年".length());
            List<Map<String,Object>> saleList = new SalesDao().saleList(year,month);
            req.setAttribute("saleList",saleList);
            req.getRequestDispatcher("sales/index.jsp").forward(req,resp);
        } catch (SQLException | StringIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}
